﻿package server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class ResTable extends UnicastRemoteObject implements ResTableInterface{

/*	private int tableNumber;
	private int tableSize;
	private boolean tableState;
*/	
	private Data data;
	
	public ResTable()throws RemoteException{
		data=new Data();
	}

	public int getTableNumber(int size)throws RemoteException{
		String sql;
		ResultSet rs=null;
		int id=0;
		
		sql="select * from restable where size=" + size+" and status<>1";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				id=rs.getInt("id");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return id;
	}
	

	public boolean addTableNumber(int tableNumber,int size)throws RemoteException, SQLException {
		/////////////////////����������ʱ��  ��ʼ������״̬Ϊ0
		String sql1;
		String sql2;
		ResultSet rs=null;
		int re=0;
		
		sql1="select * from restable where id="+tableNumber;
		sql2="insert into restable values("+tableNumber+","+size+",0)";
		
		rs=data.getResultSet(sql1);
		if(rs.next()){
			return false;
		}
		
		re=data.update(sql2);
		
		if(re==1){
			return true;
		}
		return false;
	}
	public int getTableSize(int tableNum)throws RemoteException {
		String sql;
		ResultSet rs=null;
		int size=0;
		
		sql="select * from restable where id=" + tableNum;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				size=rs.getInt("size");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return size;
	}
	public boolean setTableSize(int tableNum,int tableSize)throws RemoteException{
		String sql;
		int re=0;
		
		sql="update restable set size = "+tableSize+" where id="+tableNum;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
	public boolean isTableState()throws RemoteException{
		String sql;
		ResultSet rs=null;
		
		sql="select * from restable where status=0";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				return true;
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return false;
	}
	public boolean setTableState(int tableNum,int tableState) throws RemoteException{
		String sql;
		int re=0;
		
		sql="update restable set status = "+tableState+" where id="+tableNum;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}



public int getTableListSize()throws RemoteException{
	String sql;
	ResultSet rs=null;
	int sum=0;
	
	sql="select count(*) as sum from restable";
	rs=data.getResultSet(sql);
	try {
		if(rs.next())
			sum=rs.getInt("sum");


		} catch (SQLException e) {

		e.printStackTrace();
		}
		
	return sum;
}
	


}
